import {Component} from "@angular/core";
import {Events, IonicPage, LoadingController, NavController, NavParams} from "ionic-angular";
import {RosterDetailProvider} from "../../../providers/activity/roster-detail/roster-detail.provider";
import {RosterDetailModel} from "../../../model/pages/activity/roster-detail/roster-detail.model";
import {CallNumber} from "@ionic-native/call-number";
import {RosterListProvider} from "../../../providers/activity/roster-list/roster-list.provider";
import {CallNumberService} from "../../../services/native/call-number/call-number.service";
import {ClientNoteLogModel} from "../../../model/pages/client/client-note/client-note-log/client-note-log.model";
import {ClientNoteEditProvider} from "../../../providers/client/client-note/client-note-edit/client-note-edit.provider";
import {LoadingService} from "../../../services/common/loading.service";
import {ClientsAddPage} from "../../client/client-add/client-add";
import {TrackStatus} from "../../../model/pages/activity/roster-list/roster-list-item.model";
import {AddStaffDetailPage} from "../../add-staff/add-staff-detail/add-staff-detail";

@IonicPage()
@Component({
    selector: 'page-roster-detail',
    templateUrl: 'roster-detail.html',
    providers: [RosterDetailProvider, CallNumber,
        ClientNoteEditProvider, LoadingController,
        LoadingService,
        RosterListProvider, CallNumberService]
})
export class RosterDetailPage {

    constructor(private rosDePro: RosterDetailProvider,
                private navParams: NavParams,
                private callNumber: CallNumber,
                private rosterPro: RosterListProvider,
                private callNumberService: CallNumberService,
                private loadingService: LoadingService,
                private navCtrl: NavController,
                private events: Events) {
    }

    //客户Id
    private clientId;
    private detail: RosterDetailModel = new RosterDetailModel();
    //能否打电话
    private canCall = true;
    //标记当前是 市场活动名单还是增员活动名单跳转来的页面
    private classify;

    ionViewDidLoad() {
        this.clientId = this.navParams.get('clientId');
        this.classify = this.navParams.get('classify');
        this.getClientActivityDetail();
        this.events.subscribe('refresh roster1', () => {
            this.switchUserStatus();
        });
    }

    getClientActivityDetail() {
        this.loadingService.showLoading();
        let start = Date.now();
        this.rosDePro.getActivityClientDetail({
            id: this.clientId,
            classify: this.classify
        }).subscribe((res: RosterDetailModel) => {
            this.detail = res;
            this.dismissLoading(start);
        }, err => {
            this.dismissLoading(start);
        });
    }

    dismissLoading(start) {
        let spend = Date.now() - start;
        if (spend < 1000) {
            setTimeout(() => {
                this.loadingService.dismissLoading();
            }, 1000 - spend);
        } else {
            this.loadingService.dismissLoading();
        }
    }

    callPhone(ev) {
        if (this.detail.tel && this.canCall) {
            this.canCall = false;
            let clientLogModel = new ClientNoteLogModel();
            clientLogModel.clientType = this.classify === 0 ? this.detail.clientType : 3;
            clientLogModel.noteType = 4;
            clientLogModel.content = '拨打电话';
            clientLogModel.contentType = 1;
            clientLogModel.clientNumber = this.detail.id + '';

            if (this.classify === 0) {
                this.callNumberService.makeCall(this.detail.tel + '',
                    clientLogModel, true, {
                        name: this.detail.name,
                        customerId: this.detail.id,
                        tel: this.detail.tel
                    },
                    () => {
                    },
                    () => {
                    },
                    () => {
                        this.switchUserStatus();
                        this.canCall = true;
                    });
            } else {
                this.callNumberService.makeCall(this.detail.tel + '', clientLogModel, false, {
                        name: this.detail.name,
                        customerId: this.detail.id,
                        tel: this.detail.tel
                    },
                    () => {
                    },
                    () => {
                    },
                    () => {
                        this.canCall = true;
                    });
            }
        }
        ev.stopPropagation();
    }

    switchUserStatus() {
        this.detail.activitysInProgress.forEach((item) => {
            if (item.trackStatus === TrackStatus.notFollow) {
                item.trackStatus = TrackStatus.contacted;
            }
        });
        this.events.publish('refresh roster', this.detail);
    }

    //去添加客户界面
    toClientAdd() {
        this.navCtrl.push('ClientsAddPage', {
            addData: {
                tel: this.detail.tel,
                name: this.detail.name
            }
        })
    }

    //跳转客户详情页
    goToClientDetail() {
        if (this.classify === 0) {
            if (this.detail.isInPhoneBook) {
                if (this.detail.clientType == '2') {
                    this.navCtrl.push('HolderClientDetailPage', {
                        clientNum: this.detail.id
                    });
                } else if (this.detail.clientType == '1') {
                    this.navCtrl.push('ClientDetailPage', {
                        clientNum: this.detail.id
                    });
                }
            }
        } else {
            this.navCtrl.push('AddStaffDetailPage', {
                recruit: {
                    recruitId: this.detail.id
                }
            });
        }
    }
}
